package testhibernate;

import java.util.HashMap;
import java.util.List;

import mappedClass.Task;
import mappedClass.TaskExpense;
import mappedClass.Team;
import mappedClass.User;
import mappedClass.UserType;
import mappedClass.WinaproachRecord;

import org.hibernate.Session;

import classDAO.TaskDAO;
import classDAO.UserDAO;

public class HibernateTest {

	public static void main(String[] args) {

		/** WE OPEN THE SESSION **/
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		
		/** WE START THE TRANSACTION **/
		session.beginTransaction();

//		/** CREATE AND SAVE A NEW USERTYPE **/
//		UserType newUT = new UserType(0, "admin");
//		session.save(newUT);
////		UserType ut = (UserType)session.get(UserType.class, 0);
//		/** CREATE AND SAVE A NEW USER **/
//		User newUser = new User(newUT, "luc5", "luc5", "luc5", "luc5", "luc5");
//		session.save(newUser);
//		
//		/** CREATE AND SAVE A NEW TEAM **/
//		Team newTeam = new Team(newUser, "New Team");
//		session.save(newTeam);
//		
//		/** CREATE AND SAVE USER IN THE TEAM **/
//		UserTeamHistoryId newUTHId = new UserTeamHistoryId(newTeam.getId(), newUser.getId());
//		UserTeamHistory newUTH = new UserTeamHistory(newUTHId, newTeam, newUser);
//		session.save(newUTH);
//		
//		/** CREATE AND SAVE A NEW TASKTYPE **/
//		TypeTask newTT = new TypeTask("Winaproach");
//		session.save(newTT);
//		
////		/** CREATE AND SAVE A NEW TASK **/
////		Task newTask = new Task(newTT, "It is a new task");
////		session.save(newTask);
//		
//		/** CREATE AND SAVE A NEW WinaproachReport **/
//		WinaproachRecord newWaR = new WinaproachRecord(newTT, "It is a new task", "new Winaproach", 123456);
//		session.save(newWaR);

//		/** ASSIGN A TASK TO A USER **/
//		Task task = (Task)session.get(Task.class, 13);
//		User user = (User)session.get(User.class, 36);
//		UserTaskId newUTId = new UserTaskId(task.getId(), user.getId());
//		UserTask newUTask = new UserTask(newUTId, task, user);
//		session.save(newUTask);
//		
//		/** ASSIGN A TASK TO A TEAM **/
//		Set<Task> setTask = new HashSet<Task>();
//		setTask.add(task);
//		Team team = (Team)session.get(Team.class, 9);
//		team.setTasks(setTask);

		/** WE COMMIT ALL **/
		session.getTransaction().commit();
		
		UserDAO userDAO = new UserDAO(); 
		userDAO.isUserPresent("luc5", "luc5");
		int id = userDAO.getIdByName("luc5");
		User user = (User)userDAO.getById(id);
		System.out.println("ID user : "+ id);
		System.out.println("user : "+ user.getLogin() + "  " + user.getId());
		UserType usertype = (UserType)userDAO.getUserType("luc5");
		System.out.println("UserType name : " + usertype.getName());
		
		List<User> users = userDAO.userByTeam("New Team");
		
		for (User user1 : users)
		{
			System.out.println(user1.getLogin() + "  " + user1.getId());
		}
		
		List<User> users1 = userDAO.userByTaskAndTeam("new Winaproach", "New Team");
		for (User user1 : users1)
		{
			System.out.println(user1.getLogin() + "  " + user1.getId());
		}
		
		int nbUser = userDAO.numberUsersByUserType("admin");
		System.out.println(nbUser);
		
		
		TaskDAO taskDAO = new TaskDAO();
		
		HashMap<Integer, Integer> listA = taskDAO.displayPoint("2014-07-10","luc5");
		HashMap<Integer, Integer> listAB = taskDAO.totalPoint("luc5", null);
		
		
		/********************LIST ALL TASK BY TEAM******************
		List<Object[]> listTaskTeam = taskDAO.taskByTeam("New Team");
		System.out.println("SIZE : " + listTaskTeam.size());
		for (Object[] row : listTaskTeam) {
			System.out.println("----------------------");
			System.out.println("ID : " +((Task)row[1]).getId());
			System.out.println("COMPLETION : " +((TaskExpense)row[0]).getCompletionTime());
			System.out.println("DESCRIPTION : " +((TaskExpense)row[0]).getTask().getDescription());
			System.out.println("NAME : " + ((Task)row[1]).getId());
			
			if (((Task)row[1]).getTypetask().getName().equals("Winaproach"))
			{
				System.out.println("INFORMATION WINAPROACH : " + ((WinaproachRecord)row[1]).getName() + "  " +((WinaproachRecord)row[1]).getRecordNumber());
			}
		}
		************************************************************/
		
		
		//taskDAO.workedTask("luc5");
		
		//taskDAO.addWinaproachRecord(123456, "test win aproach", "Winaproach", "Ceci est un test");
		
		//taskDAO.addUserTask(123456, "test win aproach", "10-07-2014", "luc5");
		
		//taskDAO.completionTime("11-07-2014", "test win aproach", 10);
		
		
		
		/********************INFORMATION TASK TEAM******************
		List<Object[]> listInfoTaskTeam = taskDAO.informationTaskTeam ();
		for (Object[] row : listInfoTaskTeam) {
			System.out.println("----------------------");
			System.out.println("TYPE TASK : " +(((Task)row[0]).getTypetask().getName()));
			System.out.println("TEAM : " +((Team)row[1]).getName());
			System.out.println("PROJECT MANAGER : " +((Team)row[1]).getUser().getLogin());
			System.out.println("% COMPLETED : " + ((TaskExpense)row[2]).getCompletionTime());
			
			if (((Task)row[0]).getTypetask().getName().equals("Winaproach"))
			{
				System.out.println("INFORMATION WINAPROACH : " + ((WinaproachRecord)row[0]).getName() + "  " +((WinaproachRecord)row[0]).getRecordNumber());
			}
		}
		************************************************************/
		
		
		
		
		/*******************CLOSED TASK*******************************
		List<Object> listClosedTask = taskDAO.terminatedTask();
		for (Object row : listClosedTask) {
			System.out.println("----------------------");
			System.out.println("TYPE TASK : " +(((Task)row).getTypetask().getName()));
			
			if (((Task)row).getTypetask().getName().equals("Winaproach"))
			{
				System.out.println("INFORMATION WINAPROACH : " + ((WinaproachRecord)row).getName() + "  " +((WinaproachRecord)row).getRecordNumber());
			}
		}
		*************************************************************/
		
		
		
		
		/****************TIME SPENT BY DAY**********************
		int val = taskDAO.timeSpentByDay ("10-07-2014", "luc5");
		System.out.println("VALUE : " + val);
		*******************************************************/
		
		
		
		//		/** REQUETE SQL **/
//		
//		
//		/** GET THE LIST OF TEAM **/
//		Query reqTeam = session.createQuery("From Team ");
//		List<Team> listTeam = reqTeam.list();
//		// Print the number of team
//		System.out.println("num of team:" + listTeam.size());
//		// Print the name of each team
//		for (Team next : listTeam) {
//			System.out.println("next team: " + next.getName() + " --  List of tasks :  " + next.displayTasks(next.getTasks()));
//		}
//
//		
//		/** GET THE LIST OF USER **/
//		Query q = session.createQuery("From User ");
//		List<User> resultList = q.list();
//		// Print the number of user
//		System.out.println("num of user:" + resultList.size());
//		// Print the first name of each user
//		for (User next : resultList) {
//			System.out.println("next user: " + next.getFirstName());
//		}
//		
//		
//		/** GET THE LIST OF TYPE TASK **/
//		Query q1 = session.createQuery("From TypeTask ");
//		List<TypeTask> resultList1 = q1.list();
//		// Print the number of type task
//		System.out.println("num of TypeTask:" + resultList1.size());
//		// Print the name of each type task
//		for (TypeTask next : resultList1) {
//			System.out.println("next TypeTask: " + next.getName());
//		}
//		
//		
//		/** GET THE LIST OF WINAPROACH RECORD **/
//		Query q2 = session.createQuery("From WinaproachRecord ");
//		List<WinaproachRecord> resultList2 = q2.list();
//		// Print the number of WinaproachRecord
//		System.out.println("num of WinaproachRecord:" + resultList2.size());
//		// Print the name and the description of each WinaproachRecord
//		for (WinaproachRecord next : resultList2) {
//			System.out.println("next WinaproachRecord: " + next.getName()
//					+ next.getTask().getDescription());
//		}
//		
//
//		/** GET THE LIST OF WINAPROACH RECORD, WITH TYPETASK AND TASK INFORMATION **/
//		Query q3 = session.createQuery("FROM WinaproachRecord AS wr");
//		// From WinaproachRecord, TypeTask as tt, Task WHERE tt.name = 'CR' AND
//		// typeTaskId = tt.id
//		List<WinaproachRecord> resultList3 = q3.list();
//		// Print the number of rows
//		System.out.println("num of WinaproachRecord:" + resultList3.size());
//		// Print the name of each field
//		// next[0] is related to the first table
//		// next[1] is related to the second table
//		// etc ...
//		for (WinaproachRecord next : resultList3) {
//			System.out.println("next WinaproachRecord: "
//					+ next.getName() + "  " + next.getTask().getDescription() + "  " + next.getDescription());
//		}
		
		
	}
}
